package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.constant.Constants;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.shiro.ShiroUser;
import com.jl.commons.utils.CommonUtils;
import com.jl.commons.utils.CopyBean;
import com.jl.mapper.*;
import com.jl.model.Component;
import com.jl.model.Device;
import com.jl.model.DeviceComponent;
import com.jl.model.Model;
import com.jl.model.in.ModelIn;
import com.jl.service.IModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备型号表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class ModelServiceImpl extends ServiceImpl<ModelMapper, Model> implements IModelService {

    @Autowired
    private ModelComponentMapper modelComponentMapper;

    @Autowired
    private ComponentMapper componentMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceComponentMapper deviceComponentMapper;

    @Override
    public void selectModelDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<Model> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public boolean saveModelWithComponents(Model model) {
        boolean insert = baseMapper.insert(model) > 0;
        if (insert) {
            return modelComponentMapper.insertModelComponentList(model.getId(), model.getNewComponentList()) > 0;
        }
        return false;
    }


    @Override
    public boolean updateModelWithComponents(ShiroUser currentUser, Model model) {
        model.setGmtModified(new Date());
        model.setModifyId(currentUser.getId());
        boolean update = baseMapper.updateById(model) > 0;
        Long modelId = model.getId();
        if (update) {
            //判断是否编辑了配件信息
            if (model.isEditComponent()) {
                //修改绑定配件信息
                Map<String, List<Long>> map = CommonUtils.GetListDifferent(model.getOldComponentList(), model.getNewComponentList());
                List<Long> addComponents = map.get(Constants.LIST_DIFFERENT_ADD);
                List<Long> reduceCommponent = map.get(Constants.LIST_DIFFERENT_REDUCE);
                //获取所有的该类型的设备
                List<Device> deviceList = deviceMapper.selectDeviceWithModelId(modelId);
                /*
                    1、获得所有新增加的配件和减少的配件。
                    2、先将新增加的数据插入到model_component表中，将减少的内容从model_component表中删除。
                    3、获取所有属于该类型的设备的相关信息，如启用时间等
                    4、将新增加的配件数据插入到device_component的表中，将减少的内容从device_component表中删除。
                    5、这里的配件启用时间默认是重设备的启用时间。设备启用了零件才能启用。（也可以单独编辑）
                 */
                //对于新增加的就直接插入就好
                boolean addComponent = true;
                if (null != addComponents && !addComponents.isEmpty()) {
                    addComponent = modelComponentMapper.insertModelComponentList(modelId, addComponents) > 0;
                    List<Component> addComponentList = componentMapper.selectComponentByIds(addComponents);
                    for (Device device:deviceList){
                        List<DeviceComponent> deviceComponentList = CopyBean.componentsToDeviceComponents(currentUser.getId(),device,modelId,addComponentList);
                        deviceComponentMapper.insertBatch(deviceComponentList);
                    }
                }
                if (null != reduceCommponent && !reduceCommponent.isEmpty()) {
                    //如果有减少的则必须要删除在该类型的device_component中的符合条件的数据
                    deviceComponentMapper.deleteDeviceComponentListWithModelIdByComponentIds(modelId,reduceCommponent);
                    return addComponent && modelComponentMapper.deleteModelComponentList(modelId, reduceCommponent) > 0;
                }
                return addComponent;
            } else {
                //如果没有编辑配件信息则不做任何修改
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean isModelNameUsed(String name) {
        return baseMapper.selectModelNameCount(name)>0;
    }

    @Override
    public boolean isModelNameCanUse(String name, Long modelId) {
        ModelIn modelIn = baseMapper.selectModelInByName(name);
        if(modelIn == null){
            //如果找不到数据则表示该数据没有被使用
            return true;
        }else{
            if(modelIn.getId().equals(modelId)){
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }

    @Override
    public Model selectModelByDeviceId(Long deviceId) {
        return baseMapper.selectModelByDeviceId(deviceId);
    }
}
